﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OneShoppingDAL;
using OneShoppingBLL;
using OneShoppingUtility;
using OneShoppingModel;

public partial class Backstage_Pages_sysmng_roleMenuAlter : BasePage
{
    int rolemenuid;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (role.RoleId != 1)
        {
            Response.Redirect("login.html");
        }
        if (!IsPostBack)
        {
            BindDDL();
            try
            {
                rolemenuid = Convert.ToInt32(Request["id"]);
                Session["rolemenuid"] = rolemenuid;
                RoleMenu rm = RoleMenuBLL.GetInstance().GetEntity(r => r.Id == rolemenuid);
                rolelist.SelectedValue = rm.RoleId.ToString();
                //获取二级菜单ID  和 一级菜单ID
                OneShoppingModel.Menu m=MenuBLL.GetInstance().GetEntity(r=>r.MenuId==rm.MenuId);
                OneShoppingModel.Menu pm = MenuBLL.GetInstance().GetEntity(r => r.MenuId == m.ParentMenu);

                menulist.SelectedValue = pm.MenuId.ToString();

                UtilityDataBind.DropDownListBindData<OneShoppingModel.Menu>(ref twomenulist,
                    MenuBLL.GetInstance().GetList(r => r.ParentMenu == pm.MenuId), "MenuName", "MenuId", "二级菜单");
                twomenulist.SelectedValue = m.MenuId.ToString();

            }
            catch
            {
                UtilityMessageBox.ShowAndRedirect(this, "非法访问", "roleMenuManager.aspx");
            }
        }
        else
        {
            rolemenuid = Convert.ToInt32(Session["rolemenuid"]);
        }
    }
    //绑定数据源
    private void BindDDL()
    {
        UtilityDataBind.DropDownListBindData<Role>(ref rolelist,
            RoleBLL.GetInstance().GetList(), "RoleName", "RoleId", "角色");
        UtilityDataBind.DropDownListBindData<OneShoppingModel.Menu>(ref menulist,
            MenuBLL.GetInstance().GetList(r => r.ParentMenu == 0), "MenuName", "MenuId", "一级菜单");
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (rolelist.SelectedValue == "-1")
        {
            msg_role.InnerText = "请选择角色";
            msg_role.Visible = true;
            return;
        }
        if (menulist.SelectedValue == "-1")
        {
            msg_menu.InnerText = "请选择一级菜单";
            msg_menu.Visible = true;
            return;
        }
        if (twomenulist.SelectedValue == "-1")
        {
            msg_twomenu.InnerText = "请选择二级菜单";
            msg_twomenu.Visible = true;
            return;
        }
        int menuid = Convert.ToInt32(menulist.SelectedValue);
        int roleid = Convert.ToInt32(rolelist.SelectedValue);
        int twomenuid = Convert.ToInt32(twomenulist.SelectedValue);

        RoleMenu t = RoleMenuBLL.GetInstance().GetEntity(r => r.MenuId == menuid && r.RoleId == roleid);
        if (t == null)
        {
            RoleMenu onerm = new RoleMenu
            {
                MenuId = menuid,
                RoleId = roleid
            };
            RoleMenuBLL.GetInstance().Insert(onerm);
        }
        RoleMenu q = RoleMenuBLL.GetInstance().GetEntity(r => r.MenuId == twomenuid && r.RoleId == roleid);
        if (q != null)
        {
            msg_twomenu.InnerText = "角色权限已存在";
            msg_twomenu.Visible = true;
            return;
        }
        RoleMenu rm = RoleMenuBLL.GetInstance().GetEntity(r => r.Id == rolemenuid);
        rm.MenuId =Convert.ToInt32(twomenulist.SelectedValue);
        rm.RoleId = Convert.ToInt32(rolelist.SelectedValue);
        bool rrm = RoleMenuBLL.GetInstance().Update(rm);
        if (rrm)
        {
            UtilityMessageBox.ShowAndRedirect(this, "更新角色权限成功", "roleMenuManager.aspx");
        }
        else
        {
            UtilityMessageBox.ShowAndRedirect(this, "更新角色权限失败", "roleMenuManager.aspx");
        }
    }
    protected void btnReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("roleMenuManager.aspx");
    }
    protected void menulist_SelectedIndexChanged(object sender, EventArgs e)
    {
        int parentid = Convert.ToInt32(menulist.SelectedValue);
        UtilityDataBind.DropDownListBindData<OneShoppingModel.Menu>(ref twomenulist,
            MenuBLL.GetInstance().GetList(r => r.ParentMenu == parentid), "MenuName", "MenuId", "二级菜单");
    }
}